package cn.broadsense.web.controller.biz;

import cn.broadsense.biz.domain.Channel;
import cn.broadsense.biz.domain.ChannelGovernance;
import cn.broadsense.biz.domain.ChannelGovernanceDetails;
import cn.broadsense.biz.enums.ChannelField;
import cn.broadsense.biz.enums.ChannelFieldEnum;
import cn.broadsense.biz.service.ChannelGovernanceDetailsService;
import cn.broadsense.biz.service.ChannelGovernanceService;
import cn.broadsense.biz.vo.ChannelGovernanceVo;
import cn.broadsense.biz.vo.ChannelVo;
import cn.broadsense.common.core.controller.BaseController;
import cn.broadsense.common.core.domain.R;
import cn.broadsense.common.core.page.TableDataInfoResult;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 点位治理数据
 *
 * @author: pengxin
 * @create: 2024-04-11 17:23
 * @Version 1.0
 **/

@RestController
@Api(tags = "点位治理数据")
@RequestMapping("/channel/governance")
@AllArgsConstructor
public class ChannelGovernanceController extends BaseController {

    private final ChannelGovernanceService governanceService;

    private final ChannelGovernanceDetailsService governanceDetailsService;

    @GetMapping("/list")
    @ApiOperation("获取点位治理数据列表")
    public R<TableDataInfoResult<List<ChannelGovernance>>> list(@RequestParam(required = false) @ApiParam(value = "治理状态 0=未治理 1=已治理") Integer status,
                                                                @RequestParam(required = false) @ApiParam(value = "同步状态 0=未同步 1=已同步") Integer syncStatus,
                                                                @RequestParam(required = false) @ApiParam(value = "开始时间（yyyy-MM-dd HH:mm:ss)") String startTime,
                                                                @RequestParam(required = false) @ApiParam(value = "结束时间（yyyy-MM-dd HH:mm:ss)") String endTime
    ) {
        startPage();
        List<ChannelGovernance> list = governanceService.lambdaQuery()
                .eq(status != null, ChannelGovernance::getStatus, status)
                .eq(syncStatus != null, ChannelGovernance::getSyncStatus, syncStatus)
                .between(startTime != null && endTime != null, ChannelGovernance::getCreateTime, startTime, endTime)
                .list();
        return R.ok(getDataTableResult(list));
    }

    @GetMapping("/{id}")
    @ApiOperation("详情")
    public R<ChannelGovernance> details(@PathVariable Long id) {
        return R.ok(governanceService.getById(id));
    }

    @GetMapping("/details/{id}")
    @ApiOperation("明细详情")
    public R<Channel> recDetails(@PathVariable Long id) {
        Channel channel = governanceDetailsService.getRecDetails(id);
        return R.ok(channel);
    }


    @GetMapping("/details/list/{id}")
    @ApiOperation("获取点位治理结果详情列表")
    public R<TableDataInfoResult<List<ChannelGovernanceDetails>>> detailsList(@PathVariable Long id) {
        startPage();
        List<ChannelGovernanceDetails> list = governanceDetailsService.lambdaQuery()
                .eq(ChannelGovernanceDetails::getChannelGovernanceId, id)
                .in(ChannelGovernanceDetails::getChangeStatus,0,1)
                .list();
        return R.ok(getDataTableResult(list));
    }


    @GetMapping("/filedList")
    @ApiOperation("获取字段列表")
    public R<List<ChannelField>> filedList() {
        return R.ok(Arrays.stream(ChannelFieldEnum.values())
                .map(ChannelField::fromChannelFieldEnum)
                .collect(Collectors.toList()));
    }

    @PostMapping("/governanceSetting")
    @ApiOperation("数据治理设置")
    public R<String> governanceSetting(@Validated @RequestBody ChannelGovernanceVo channelGovernanceVo) {
        governanceService.governanceSetting(channelGovernanceVo);
        return R.ok("", "数据治理处理中，请稍后刷新页面查看");
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除列表数据")
    public R<String> remove(@PathVariable Long id) {
        governanceService.deleteById(id);
        return R.ok();
    }

    @PostMapping("/changeSync/{id}")
    @ApiOperation("变更同步")
    public R<String> changeSync(@PathVariable Long id) {
        governanceService.changeSync(id);
        return R.ok();
    }

    @PostMapping("/changeSyncAll")
    @ApiOperation("全量同步")
    public R<String> changeSyncAll() {
        governanceService.changeSyncAll();
        return R.ok();
    }

    @PutMapping("/channelGovernanceDetails/{id}")
    @ApiOperation("修改治理点位")
    public R<String> editChannel(@PathVariable Long id, @RequestBody ChannelVo channelVo) {
        LambdaUpdateWrapper<ChannelGovernanceDetails> updateQuery = new LambdaUpdateWrapper<>();
        updateQuery.eq(ChannelGovernanceDetails::getId, id);
        updateQuery.set(ChannelGovernanceDetails::getChangeStatus, 1);
        updateQuery.set(ChannelGovernanceDetails::getChangeJson, JSONObject.toJSONString(channelVo));
        boolean update = governanceDetailsService.update(updateQuery);
        if (!update) {
            return R.fail("数据不存在，修改失败");
        }
        return R.ok();
    }

    @PutMapping("/complete/{id}")
    @ApiOperation("完成治理")
    public R<String> complete(@PathVariable Long id){
        LambdaUpdateWrapper<ChannelGovernance> updateQuery = new LambdaUpdateWrapper<>();
        updateQuery.eq(ChannelGovernance::getId, id);
        updateQuery.set(ChannelGovernance::getStatus, 1);
        boolean update = governanceService.update(updateQuery);
        if (!update) {
            return R.fail("数据不存在，完成治理失败");
        }
        return R.ok();
    }

    @DeleteMapping("/channel/{id}")
    @ApiOperation("删除明细点位数据")
    public R<String> removeDetailId(@PathVariable Long id) {
        governanceDetailsService.deleteId(id);
        return R.ok();
    }
}
